package com.runfast.waimai.web.controller.base;

import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/server/home")
public class LoginController {
    @Autowired
    private BaseUserService baseUserService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    @Autowired
    private WaiMaiRunfastAgentaccountsService waiMaiRunfastAgentaccountsService;
    @Autowired
    private SysUserTokenService sysUserTokenService;
    @Autowired
    private RunfastBusinessAccountsService runfastBusinessAccountsService;
    @Autowired
    private RunfastBusinessService runfastBusinessService;
     /*** 登录*/
    @PostMapping("/login")
    public Result login(@RequestBody BaseUser form){
        BaseUser user =null;
        if(form!=null && form.getType()!=null && StringUtils.isNotBlank(form.getUsername())&&StringUtils.isNotBlank(form.getPassword()) ){
            if (form.getType() == 1) {
                //后台管理用户登录
                //用户信息
                user = baseUserService.checkByUsername(form.getUsername());
                if(user!=null){
                    //if (StringUtils.isNotBlank(user.getPassword()) && user.getPassword().equals(DigestUtils.md5Hex(form.getPassword()))) {
                    if (StringUtils.isNotBlank(user.getPassword()) && user.getPassword().equalsIgnoreCase(form.getPassword())) {
                        user.setType(1);
                    }else{return Result.fail(null,"用户名或密码不正确！");}
                }else{return Result.fail(null,"用户名或密码不正确！");}
            } else if (form.getType() == 2) {
                //代理商登录
                WaiMaiRunfastAgentaccounts runfastAgentaccounts = waiMaiRunfastAgentaccountsService.findByaccounts(form.getUsername());//AgentAccounts查找代理商账号
                if(runfastAgentaccounts!=null){
                    if (StringUtils.isNotBlank(runfastAgentaccounts.getPassword())  && runfastAgentaccounts.getPassword().equalsIgnoreCase(form.getPassword())) {
                        WaiMaiRunfastAgentbusiness runfastAgentbusiness = waiMaiRunfastAgentbusinessService.selectByPrimaryKey(runfastAgentaccounts.getAgentid());//AgentBusiness代理商
                        if (runfastAgentbusiness != null) {
                            user=new BaseUser();
                            System.out.println(runfastAgentaccounts.getId());
                            user.setType(2);
                            user.setPassword(runfastAgentaccounts.getPassword());
                            user.setId(runfastAgentaccounts.getId());
                        }else{return Result.fail(null,"不存在对应的代理商！");}
                    }else{return Result.fail(null,"用户名或密码不正确！");}
                }else{return Result.fail(null,"用户名或密码不正确！");}
            }else if (form.getType() == 3) {
                //商家用户
                RunfastBusinessAccounts businessAccounts = runfastBusinessAccountsService.findByAccounts(form.getUsername());
                if(businessAccounts!=null){
                    if (StringUtils.isNotBlank(businessAccounts.getPassword())  && businessAccounts.getPassword().equalsIgnoreCase(form.getPassword())) {
                        RunfastBusiness runfastBusiness = runfastBusinessService.selectByPrimaryKey(businessAccounts.getBusinessid());
                        if (runfastBusiness != null) {
                            user = new BaseUser();
                            user.setId(businessAccounts.getId());
                            user.setPassword(businessAccounts.getPassword());
                            user.setType(3);
                        }else{return Result.fail(null,"不存在对应的商家！");}
                    }else{return Result.fail(null,"用户名或密码不正确！");}
                }else{return Result.fail(null,"用户名或密码不正确！");}
            }
        }else{return Result.fail(null,"传入参数不对！");}
        //生成token，并保存到数据库
        Result r = sysUserTokenService.createToken(user.getId(),user.getType());
        return r;
    }
}
